<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>
<script type="text/javascript">
$("#back").click(function(){
	one.changeContent(one.urls[one.urls.length -2]);
});
$("#content").css("background","url("+BASEURL+"files/cp/icons/form.gif) no-repeat right bottom");
</script>

<?php
if(isset($item) && isset($item->id)){
	$action = site_url("cp/home/submitForm/".$modelName."/edit/".$item->id);
}else{
	$action = site_url("cp/home/submitForm/".$modelName."/add");
}
?>
<div id="form-container">
<?php
echo form_open_multipart($action);
?>
	<?php if(in_array("language",$fields)): ?>
		<!-- div class="<?php echo (isset($item) && $item->language == "du")?"currentLanguageTab":"languageTab" ?>" id="du" <?php if(isset($item) && $item->language == "en" || !isset($item)): ?>onclick="setFormLangauge(this.id,'home','<?php echo $modelName; ?>',document.getElementById('language_relation').value);"<?php endif; ?>>Dutch</div>
		<div class="<?php echo (isset($item) && $item->language == "en" || !isset($item))?"currentLanguageTab":"languageTab" ?>" id="en" <?php if(isset($item) && $item->language == "du"): ?>onclick="setFormLangauge(this.id,'home','<?php echo $modelName; ?>',document.getElementById('language_relation').value);"<?php endif; ?>>English</div -->
		<input type="hidden" name="language" id="language" value="<?php echo (isset($item))?$item->language:"en" ?>">
		<input type="hidden" name="language_relation" id="language_relation" value="<?php echo (isset($item) && isset($item->language_relation))?$item->language_relation:time(); ?>">
	<?php endif; ?>
	<input type="hidden" name="backAfterSubmit" id="backAfterSubmit" value="<?php echo "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]; ?>" />
	<table>
		<?php foreach($fields as $field=>$specifications): if($specifications != "language" && $specifications != "language_relation"): ?>
			<tr>
				<td width="100" nowrap="nowrap"><?php echo $this->lang->line($field); ?> :</td>
				<td>
				<?php if($specifications["type"] == "upload"): ?>
					<input type="file" name="<?php echo $field; ?>" id="<?php echo $field; ?>" />
					<?php if(isset($item) && isset($item->$field)): ?>
						<br>
						<?php $ar = $item->$field; if(is_array($ar) && $ar["type"] == "image"): ?>
							<img src="<?php echo $ar["path"]?>" />
						<?php elseif(is_array($ar)): ?>
							<a href="<?php echo $ar["path"]; ?>"><?php echo $ar["path"]; ?></a>
						<?php endif; ?>
					<?php endif; ?>
				<?php elseif($specifications["type"] == "password"): ?>
					<input type="text" <?php echo (isset($specifications["readonly"]))?'readonly="readonly"':"";?> dir="<?php echo (isset($specifications["direction"]))?$specifications["direction"]:"rtl"; ?>" name="<?php echo $field; ?>" id="<?php echo $field; ?>" value="">
				<?php elseif($specifications["type"] == "timeView"): ?>
					<input type="text" <?php echo (isset($specifications["readonly"]))?'readonly="readonly"':"";?> dir="<?php echo (isset($specifications["direction"]))?$specifications["direction"]:"rtl"; ?>" name="<?php echo $field; ?>" id="<?php echo $field; ?>" value="<?php echo (isset($item) && isset($item->$field))?$item->$field:"" ?>">
				<?php elseif($specifications["type"] == "text"): ?>
					<input type="text" <?php echo (isset($specifications["readonly"]))?'readonly="readonly"':"";?> dir="<?php echo (isset($specifications["direction"]))?$specifications["direction"]:"rtl"; ?>" name="<?php echo $field; ?>" id="<?php echo $field; ?>" value="<?php echo (isset($item) && isset($item->$field))?$item->$field:"" ?>">
				<?php elseif($specifications["type"] == "checkbox"): ?>
					<input <?php echo (isset($item) && isset($item->$field) && $item->$field)?"checked=\"checked\"":""; ?> type="checkbox" name="<?php echo $field; ?>" id="<?php echo $field; ?>" value="<?php echo $specifications["value"] ?>">
				<?php elseif($specifications["type"] == "timeSelector"): ?>
					<input dir="ltr" type="text" id="<?php echo $field; ?>" readonly="readonly" name="<?php echo $field; ?>" value="<?php echo (isset($item) && isset($item->$field))?$item->$field:"" ?>">&nbsp;<input type="button" id="showDatepicker-<?php echo $field; ?>" value="Date picker">
				<?php elseif($specifications["type"] == "xinha"): ?>
					<textarea id="<?php echo $field; ?>" name="<?php echo $field; ?>" cols="95" rows="15"><?php echo (isset($item) && isset($item->$field))?$item->$field:"" ?></textarea>
				<?php elseif($specifications["type"] == "textarea"): ?>
					<textarea id="<?php echo $field; ?>" <?php echo (isset($specifications["readonly"]))?'readonly="readonly"':"";?> name="<?php echo $field; ?>" cols="45" rows="7"><?php echo (isset($item) && isset($item->$field))?$item->$field:"" ?></textarea>
				<?php elseif($specifications["type"] == "select" || $specifications["type"] == "categorySelect"): ?>
					<select id="<?php echo $field; ?>" name="<?php echo $field; ?>">
						<option <?php echo (isset($item) && isset($item->$field) && $item->$field == "")?'selected="selected" ':"" ?>value="">---</option>
						<?php foreach($specifications["options"] as $value=>$title): ?>
							<?php if($value == $title): ?>
								<optgroup label="<?php echo $title ?>"></optgroup>
							<?php else: ?>
								<option <?php echo ((isset($item) && isset($item->$field) && $item->$field == $value) || (in_array($field,$defaultFields) && in_array($value,$defaultValues)))?'selected="selected" ':"" ?>value="<?php echo $value ?>"><?php echo $title ?></option>
							<?php endif; ?>
						<?php endforeach; ?>
					</select>
				<?php endif; ?>
				</td>
			</tr>
		<?php endif; endforeach; ?>
		<tr>
			<td colspan="2" style="text-align: center;">
				<input <?php if($validation): ?>onclick="return FormValidator.validate('form1');" <?php endif; ?>type="submit" name="submit" value="<?php echo (isset($item) && isset($item->id))?"Edit":"Add"; ?>" />&nbsp;&nbsp;<?php if(isset($item) && isset($item->id)): ?><input type="reset" name="bar" id="foo" >&nbsp;&nbsp;<?php endif;?><input value="Back" type="button" id="back" name="back" />
			</td>
		</tr>
	</table>
<?php echo form_close(); ?>
</div>
<script type="text/javascript">
	<?php if(isset($calendar) && $calendar):?>
	$(function(){
		var calInputField = "<?php echo $calendar; ?>";
		var calIfFormat = "%Y-%m-%d %H:%M";
		var calShowTime = true;
		var calTimeFormat = "24";
		var calWeekNumbers = false;
		Calendar.setup({
			inputField     :    calInputField,
			button         :    "showDatepicker-"+calInputField,
		  	ifFormat       :    calIfFormat,
		  	showsTime      :    calShowTime,
		  	dateType	   :	<?php echo ((isset($item) && (isset($item->language) && $item->language == "en")) || !isset($item) || (isset($item) && !isset($item->language)))?"'gregorian'":"'gregorian'" ?>,
		  	timeFormat     :    calTimeFormat,
			weekNumbers    :	calWeekNumbers
		});
	});
	<?php endif;
	
	if(isset($validation) && $validation): ?>
	FormValidator.clear();
	<?php foreach($fields as $field=>$specifications): if($field != "language" && $field != "language_relation" && isset($specifications["mandatory"])): ?>
	FormValidator.setElement("form1","<?php echo $field ?>","<?php echo $this->lang->line($field); ?>","<?php echo $specifications["mandatory"]["type"]; ?>",<?php echo ($specifications["mandatory"]["required"])?"true":"false"; ?>);
	<?php endif; endforeach;?>
	<?php endif; ?>
</script>